<?php
/**
 *个人中心设置类
 */
namespace App\Controller\Member;
use Common\Controller\App\CommonController;
use Common\Model\CaptchaModel;
use Common\Model\MemberAddressModel;
use Common\Model\MemberModel;
use Common\Model\MicroShopModel;
use Common\Model\TarentoModel;

class SettingController extends CommonController{

    /**
     * 个人设置首页
     */
    public function index(){
        $member_id = session('member_id');
        $model = new MemberModel();
        $info = $model->where("userid=$member_id")->field("name,logo,mobile,weixin")->find();
        $this->ajaxReturn(['code'=>200,'info'=>$info]);
    }

    /**
     * 头像上传
     */
    public function upload(){
        $member_id = session('member_id');
        $logo = I('post.logo');
        if(empty($logo)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请上传头像']);
        }
        $model = new MemberModel();
        $result = $model->where("userid=$member_id")->save(['logo'=>$logo]);
        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'头像修改成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'头像修改失败，请稍后再试']);
        }
    }

    /**
     * 用户手机号绑定
     */
    public function update_mobile(){
        $member_id = session('member_id');
        $code = I('post.code');
        $mobile = I('post.mobile');
        if(!preg_match("/^1[34578]\d{9}$/", $mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入正确的手机号码']);
        }
        if(empty($code)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入手机验证码']);
        }
        $model = new CaptchaModel();
        $result = $model->where("unique_str=$mobile and captcha=$code")->order('id desc')->find();
        $model->where("unique_str=$mobile")->delete();
        if(empty($result)){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码输入错误']);
        }
        if(time()>strtotime($result['invalid_time'])){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码已过期，请输入发送']);
        }
        $model = new MemberModel();
        $info = $model->where("mobile=$mobile")->find();
        if(empty($info)){
            $this->ajaxReturn(['code'=>300,'msg'=>'此手机号已被绑定，请重新发送']);
        }
        $result = $model->where("userid=$member_id")->save(['mobile'=>$mobile]);
        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'手机号修改成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'手机号修改失败']);
        }

    }

    /**
     * 用户昵称修改
     */
    public function update_name(){
        $member_id = session('member_id');
        $name = I('post.name');
        $data['name'] = $name;
        $model = new MemberModel();
        $result = $model->where("userid=$member_id")->save($data);
        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'昵称修改成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'昵称修改失败']);
        }
    }

    /**
     * 店铺名称修改
     */
    public function shop_update_data(){
        $member_id = session('member_id');
        $name = I('post.name');
        $model = new MicroShopModel();
        $result = $model->where("member_id=$member_id")->save(['name'=>$name]);
        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'店铺名称修改成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'店铺名称修改失败']);
        }
    }

    /**
     * 店铺头像上传
     */
    public function shop_upload(){
        $member_id = session('member_id');
        $logo = I('post.logo');
        if(empty($logo)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请上传头像']);
        }
        $model = new MicroShopModel();
        $result = $model->where("member_id=$member_id")->save(['logo'=>$logo]);
        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'头像修改成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'头像修改失败，请稍后再试']);
        }
    }

    /**
     * 新增收货地址
     */
    public function add_address(){
        $member_id = session('member_id');
        $is_edit = I('post.is_edit');
        $id    = I('post.id');
        $name = I('post.name');
        $provinceid = I('post.provinceid');
        $cityid = I('post.cityid');
        $areaid = I('post.areaid');
        $area = I('post.area');
        $address = I('post.address');
        $mobile = I('post.mobile');
        $idcard = I('post.idcard');
        $is_default = I('post.is_default');
        if(empty($name)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入正确的收货人姓名']);
        }
        if(empty($provinceid)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请选择省']);
        }
        if(empty($cityid)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请选择市']);
        }
        if(empty($areaid)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请选择区县']);
        }
        if(empty($area)){
            $this->ajaxReturn(['code'=>300,'msg'=>'地址不能为空']);
        }
        if(empty($address)){
            $this->ajaxReturn(['code'=>300,'msg'=>'详细地址不能为空']);
        }
        if(empty($mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入手机号码']);
        }
        if(!preg_match("/^1[34578]\d{9}$/", $mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入正确的手机号码']);
        }

        $model = new MemberAddressModel();
        if($is_default==1){
            $model->where("userid=$member_id")->save(['is_default'=>0]);
        }
        $data['userid'] = $member_id;
        $data['name']   = $name;
        $data['provinceid'] = $provinceid;
        $data['cityid']    = $cityid;
        $data['areaid']    = $areaid;
        $data['area']      = $area;
        $data['address']   = $address;
        $data['mobile']   = $mobile;
        $data['idcard']   = $idcard;
        $data['is_default'] = $is_default;
        $data['create_time'] = date('Y-m-d H:i:s',time());
        if($is_edit==1 && $id){
            $result = $model->where("id=$id and userid=$member_id")->save($data);
        }else{
            $result = $model->add($data);
        }
        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'收货地址添加成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'收货地址添加失败，请稍后再试']);
        }
    }
    /**
     * 收货地址修改
     */
    public function address_edit(){
        $member_id = session('member_id');
        $id = I('post.id');
        $model = new MemberAddressModel();
        $info = $model->where("userid=$member_id and id=$id")->field("id,name,provinceid,cityid,areaid,address,is_default,idcard")->find();
        $this->ajaxReturn(['code'=>200,'info'=>$info]);
    }

    /**
     * 收货地址删除
     */
    public function address_del(){
        $member_id = session('member_id');
        $id = I('post.id');
        if(empty($id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'收货地址编号错误']);
        }
        $model = new MemberAddressModel();
        $result = $model->where("userid=$member_id and id=$id")->delete();

        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'收货地址删除成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'收货地址删除失败']);
        }
    }

    /**
     * 设置修改支付密码页面
     */
    public function pay_password(){
        $member_id = session('member_id');
        $model = new MemberModel();
        $info = $model->where("userid=$member_id")->field('mobile')->find();
        if(empty($info['mobile'])){
            $this->ajaxReturn(['code'=>300,'msg'=>'你还没有绑定手机号，请先绑定手机号']);
        }
        else{
            $this->ajaxReturn(['code'=>200,'info'=>$info]);
        }
    }

    /**
     * 修改支付密码
     */
    public function pay_password_update(){
        $member_id = session('member_id');
        $pay_password = I('post.pay_password');
        $q_pay_password = I('post.q_pay_password');
        $mobile = I('post.mobile');
        $code = I('post.code');
        if(empty($pay_password) || empty($q_pay_password)){
            $this->ajaxReturn(['code'=>300,'msg'=>'支付密码和确认支付密码不能为空']);
        }
        if(empty($mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机号码不能为空']);
        }
        if(!preg_match("/^1[34578]\d{9}$/", $mobile)){
            $this->ajaxReturn(['code'=>300,'msg'=>'请输入正确的手机号码']);
        }
        if(empty($code)){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码不能为空']);
        }
        $model = new CaptchaModel();
        $result = $model->where("unique_str=$mobile and captcha=$code")->order('id desc')->find();
        $model->where("unique_str=$mobile")->delete();
        if(empty($result)){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码输入错误']);
        }
        if(time()>strtotime($result['invalid_time'])){
            $this->ajaxReturn(['code'=>300,'msg'=>'手机验证码已过期，请重新发送']);
        }

        if($pay_password!=$q_pay_password){
            $this->ajaxReturn(['code'=>300,'msg'=>'支付密码与确认支付密码不一致']);
        }
        $model = new TarentoModel();
        $result = $model->where("member_id=$member_id")->save(['pay_password'=>getEncodePayPasswrod($pay_password)]);
        if($result){
            $this->ajaxReturn(['code'=>200,'msg'=>'支付密码修改成功']);
        }else{
            $this->ajaxReturn(['code'=>300,'msg'=>'支付密码修改失败']);
        }
    }

}